<?php
class storeController extends RPHP_Controller
{
    //权限前缀
    protected $_pvprefix = "store";
    //门店列表
    public function storelist()
    {

        $priv = array($this->_pvprefix . "_sl");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;

            $data = $model->getStore();

            $this->view->data = $data;

            $this->display();

        }

    }

    //新增门店
    public function storeadd()
    {

        $priv = array($this->_pvprefix . "_sl");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $this->display();

        }

    }

    //编辑门店
    public function storeedit()
    {

        $priv = array($this->_pvprefix . "_sl");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;

            $data             = $model->getStore($_GET['storeid']);
            $this->view->data = $data;

            $this->display();

        }

    }

    //保存
    public function savestore()
    {

        $priv = array($this->_pvprefix . "_sl");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $code             = 900;

            if ($_POST['act'] == "add") {
                // var_dump($_POST);die;
                $this->view->success = "新增门店成功";
                $this->view->link    = array(
                    array('name' => '继续新增门店', 'url' => '/store/storeadd'),
                    array('name' => '返回门店列表', 'url' => '/store/storelist'),
                );

                $code = $model->saveStore($_POST);

            } elseif ($_POST['act'] == "edit") {

                $this->view->success = "编辑门店成功";
                $this->view->link    = array(
                    array('name' => '返回门店列表', 'url' => '/store/storelist'),
                );

                $code = $model->saveStore($_POST, $_POST['storeid']);

            }

            $this->view->code = $code;
            $this->displaydo();

        }

    }

    //AJAX操作
    public function storeajax()
    {

        $priv = array($this->_pvprefix . "_sl");

        $model        = $this->model;
        $auth         = $model->checkPriv($priv);
        $data['code'] = 900;
        if ($auth) {
            if ($_POST['act'] == 'getregion') {
                $rs           = $model->getRegion(intval($_POST['parent_id']));
                $data['code'] = 200;
                $data['data'] = $rs;
            } elseif ($_POST['act'] == 'searchuser') {
                $data = $model->searchWeixinUser($_POST['kw']);
            } elseif ($_POST['act'] == 'searchTitle') {
                $data = $model->searchCourseTitle($_POST['kw']);
            }

        }
        echo json_encode($data);

    }
    //库存列表
    public function stocklist()
    {

        $priv = array($this->_pvprefix . "_skt");

        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            if ($_GET['keyword'] != "") {
                $keyword = addslashes(trim($_GET['keyword']));
                if ($keyword != '') {
                    $para['keyword'] = $keyword;
                }

            }

            if ($_GET['caid'] != "") {
                $caid = intval($_GET['caid']);
                if ($caid > 0) {
                    $para['caid'] = $caid;
                }

            }
            if ($_GET['brand_id'] != "") {
                $brand_id = intval($_GET['brand_id']);
                if ($brand_id > 0) {
                    $para['brand_id'] = $brand_id;
                }

            }

            if ($_GET['store_id'] != "") {
                $store_id = intval($_GET['store_id']);
                if ($store_id > 0) {
                    $para['store_id'] = $store_id;
                }

            }
            if (isset($_GET['is_on_sale']) && $_GET['is_on_sale'] != "") {
                $is_on_sale = intval($_GET['is_on_sale']);
                if ($is_on_sale >= 0) {
                    $para['is_on_sale'] = $is_on_sale;
                }

            }
            if (intval($_GET['page']) > 0) {
                $page = intval($_GET['page']);
            } else {
                $page = 1;
            }
            $res              = $model->getStockList($para, $page);
            $this->view->data = $res;

            /*搜索的商品分类品牌供应商*/
            $this->view->cate  = $model->getCategory(0, false, 1); //商品分类
            $this->view->brand = $model->getBrand(0, 1, 1); //商品品牌
            $this->view->store = $model->getStore(); //门店
            if ($this->view->brand) {
                foreach ($this->view->brand as $r) {
                    $py               = strtoupper(mb_substr($model->pinyin->get($r['name']), 0, 1, "utf-8"));
                    $brand_new[$py][] = $r;
                }
                ksort($brand_new);
                $this->view->brand = $brand_new;
            }

            //获取页码
            if ($this->view->data['maxpage'] > 1) {
                $this->view->data['spage'] = $model->fun->spage($this->view->data['page'], $this->view->data['maxpage']);
            }
            // print_r($this->view->data);die;
            //库存权限
            $this->view->pv['stock'] = $model->checkPriv($this->_pvprefix . "_st", false, false);
            //报损权限
            $this->view->pv['damaged'] = $model->checkPriv($this->_pvprefix . "_dm", false, false);
            $this->display();
        }

    }

    
    //库存列表
    public function stock_dec_number()
    {
    
        $priv = array($this->_pvprefix . "_sd");
    
        $model        = $this->model;
        $auth         = $model->checkPriv($priv);
        if($auth){
            $this->view->auth = $auth;
            if ($_GET['keyword'] != "") {
                $keyword = addslashes(trim($_GET['keyword']));
                if ($keyword != '') {
                    $para['keyword'] = $keyword;
                }
    
            }
    
    
            if ($_GET['store_id'] != "") {
                $store_id = intval($_GET['store_id']);
                if ($store_id > 0) {
                    $para['store_id'] = $store_id;
                }
    
            }
            if ($_GET['start_time'] != "") {
                $start_time = strtotime($_GET['start_time']);
                if ($start_time > 0) {
                    $para['start_time'] = $start_time;
                }
            }
            if ($_GET['end_time'] != "") {
                $end_time = strtotime($_GET['end_time']);
                if ($end_time > 0) {
                    $para['end_time'] = $end_time;
                }
            
            }
            if (intval($_GET['page']) > 0) {
                $page = intval($_GET['page']);
            } else {
                $page = 1;
            }
            $this->view->store = $model->getStore(); //门店
            $res         = $model->getStoreDecNumber($para, $page);
            $this->view->data = $res;
   
            //获取页码
            if ($this->view->data['maxpage'] > 1) {
                $this->view->data['spage'] = $model->fun->spage($this->view->data['page'], $this->view->data['maxpage']);
            }
           
            $this->display();
        }
    
    }
    //增加库存
    public function stock_number()
    {
        $priv  = array($this->_pvprefix . "_st");
        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $goods_id         = intval($_GET['goods_id']);
            $store_id         = intval($_GET['store_id']);
            if ($_GET['ref'] != "") {
                $this->view->ref = Rfc::decode($_GET['ref']);
            } else {
                $this->view->ref = $_SERVER['HTTP_REFERER'];
            }
            $this->view->store        = $model->getStore();
            $this->view->goods_number = $model->getGoodsNumber($goods_id, $store_id);
            $this->view->goodsid      = $goods_id;
            $this->view->store_id     = $store_id;
            $this->display();
        }

    }

    //商品报损
    public function stock_damaged()
    {
        $priv  = array($this->_pvprefix . "_dm");
        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            $goods_id         = intval($_GET['goods_id']);
            $store_id         = intval($_GET['store_id']);
            if ($_GET['ref'] != "") {
                $this->view->ref = Rfc::decode($_GET['ref']);
            } else {
                $this->view->ref = $_SERVER['HTTP_REFERER'];
            }

            $this->view->goods_number = $model->getGoodsNumber($goods_id, $store_id);
            $this->view->goodsid      = $goods_id;
            $this->view->store_id     = $store_id;
            $this->display();
        }

    }
    //入库保存
    public function goodsnumber()
    {

        $priv  = array($this->_pvprefix . "_st");
        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth    = $auth;
            $this->view->code    = 900;
            $this->view->success = "保存商品库存成功";
            $this->view->link    = array(
                array('name' => '返回库存列表', 'url' => '/store/stock_number?goods_id=' . $_POST['goods_id'] . "&store_id=" . $_POST['store_id'] . "&ref=" . Rfc::encode($_POST['ref'])),
                array('name' => '返回来源页面', 'url' => $_POST['ref']),
                array('name' => '返回商品列表', 'url' => '/store/stocklist'),
            );
            $this->view->code = $model->saveGoodsNumber($_POST);
            $this->displaydo();
        }
    }

    // 保存报损
    public function savedamaged()
    {
        $priv  = array($this->_pvprefix . "_dm");
        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth    = $auth;
            $this->view->code    = 900;
            $this->view->success = "保存报损数据成功";
            $this->view->link    = array(
                array('name' => '返回来源页面', 'url' => $_POST['ref']),
                array('name' => '返回商品列表', 'url' => '/store/stocklist'),
            );
            $this->view->code = $model->saveGoodsDamaged($_POST);
            $this->displaydo();
        }
    }

    //库存 异步
    public function ajax()
    {
        $priv  = array($this->_pvprefix . "_skt");
        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {

            if ($_POST['act'] == 'ajax_goods_barcode') {
                $data = $model->getGoodsId($_POST);

            }
            echo json_encode($data);
        }

    }

    public function breakage()
    {
        $priv  = array($this->_pvprefix . "_bk");
        $model = $this->model;
        $auth  = $model->checkPriv($priv);
        if ($auth) {
            $this->view->auth = $auth;
            if ($_GET['keyword'] != "") {
                $keyword = addslashes(trim($_GET['keyword']));
                if ($keyword != '') {
                    $para['keyword'] = $keyword;
                }

            }

            if ($_GET['caid'] != "") {
                $caid = intval($_GET['caid']);
                if ($caid > 0) {
                    $para['caid'] = $caid;
                }

            }
            if ($_GET['brand_id'] != "") {
                $brand_id = intval($_GET['brand_id']);
                if ($brand_id > 0) {
                    $para['brand_id'] = $brand_id;
                }

            }

            if ($_GET['store_id'] != "") {
                $store_id = intval($_GET['store_id']);
                if ($store_id > 0) {
                    $para['store_id'] = $store_id;
                }

            }
            
            if (intval($_GET['page']) > 0) {
                $page = intval($_GET['page']);
            } else {
                $page = 1;
            }
            $res              = $model->getBreakage($para, $page);
            $this->view->data = $res;

            /*搜索的商品分类品牌供应商*/
            $this->view->cate  = $model->getCategory(0, false, 1); //商品分类
            $this->view->brand = $model->getBrand(0, 1, 1); //商品品牌
            $this->view->store = $model->getStore(); //门店
            if ($this->view->brand) {
                foreach ($this->view->brand as $r) {
                    $py               = strtoupper(mb_substr($model->pinyin->get($r['name']), 0, 1, "utf-8"));
                    $brand_new[$py][] = $r;
                }
                ksort($brand_new);
                $this->view->brand = $brand_new;
            }

            //获取页码
            if ($this->view->data['maxpage'] > 1) {
                $this->view->data['spage'] = $model->fun->spage($this->view->data['page'], $this->view->data['maxpage']);
            }
            // print_r($this->view->data);die;
            //库存权限
            $this->view->pv['stock'] = $model->checkPriv($this->_pvprefix . "_st", false, false);
            //报损权限
            $this->view->pv['damaged'] = $model->checkPriv($this->_pvprefix . "_dm", false, false);
            $this->display();
        }
    }

}
